package test5.lupeng.jf.com.teamdemo.activitys;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.bean.AtOthersBean;
import test5.lupeng.jf.com.teamdemo.bean.request.PublishBlogCommentBean;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

public class PublishCommentActivity extends AppCompatActivity {
    private static final String TAG = "PublishCommentActivity";
    private String userId;
    private String articleId;
    ArrayList<AtOthersBean> atOthersBeen =new ArrayList<>();
    private ArrayList<String> eit = new ArrayList<String>();;
    private EditText edit_comment;
    private String comment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_comment);
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        userId = sp.getString("objectId", null);

        articleId = getIntent().getStringExtra("articleId");
        Log.e(TAG, "onCreate: " + userId);
        initView();
    }


    /**
     * 初始化视图
     */
    private void initView() {
        TextView tv_back = (TextView) findViewById(R.id.tv_back);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        edit_comment = (EditText) findViewById(R.id.edit_comment);

        TextView tv_publish = (TextView) findViewById(R.id.tv_publish);
        tv_publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                comment = edit_comment.getText().toString();
               if(comment ==null||"".equals(comment)){
                   Toast.makeText(PublishCommentActivity.this, "评论内容不能为空！", Toast.LENGTH_SHORT).show();
                   return;
               }
               if(userId==null||"".equals(userId)){
                   Toast.makeText(PublishCommentActivity.this, "用户id为空，请登录之后再发表！", Toast.LENGTH_SHORT).show();
                   return;
               }

               publishComments(userId,articleId, comment, eit,"2");
            }
        });


       TextView tv_at = (TextView) findViewById(R.id.tv_at);
        tv_at.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PublishCommentActivity.this,UserListActivity.class);
                startActivityForResult(intent,222);
            }
        });
    }


    /**
     * 发表博客评论
     * @param userId        当前用户id
     * @param articleId     博客id
     * @param content       评论内容
     * @param eit           @的人
     * @param articleType   文章类型
     */
    private  void publishComments(String userId, String articleId, String content, ArrayList<String> eit,String articleType){
        String url = "https://leancloud.cn:443/1.1/functions/commetnBlog";
        PublishBlogCommentBean bean = new PublishBlogCommentBean(userId,articleId,content,eit,articleType);
        Request request = HTTPUtils.requestPost(bean,url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String jsonResult= response.body().string();
                Log.e(TAG, "onResponse: "+jsonResult );
                     if(response.code()==200){
                         runOnUiThread(new Runnable() {
                             @Override
                             public void run() {
                                 Toast.makeText(PublishCommentActivity.this, "评论成功！", Toast.LENGTH_SHORT).show();
                                 finish();
                             }
                         });
                     }
            }
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 222:
                if(resultCode==RESULT_OK){
                    atOthersBeen.clear();
                   ArrayList<AtOthersBean> been=  data.getParcelableArrayListExtra("userlist");
                    atOthersBeen.addAll(been);
                    Log.e(TAG, "onActivityResult: "+atOthersBeen.size() );
                    eit.clear();
                    comment = edit_comment.getText().toString();
                    for (int i = 0; i <atOthersBeen.size() ; i++) {
                        comment=comment+"@"+atOthersBeen.get(i).getUsername();
                        eit.add(atOthersBeen.get(i).getUserId());
                    }
                    edit_comment.setText(comment);

                }
                break;
        }
    }
}
